package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.*;
import appapi.Vo.*;
import appapi.common.ResponseBean;
import appapi.config.CurrentUser;
import appapi.entity.Zhongjiao.*;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.*;
import appapi.utils.WeChatUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 操作人员，管理人员，游客首页，通用查询Controller
 */
@Api(value = "/test", tags = "App接口")
@RestController
@RequestMapping(value = "/test")
public class AppApiController {
    @Resource
    private IAppApiService iAppApiService;
    @Resource
    private ILoginService loginService;
    @Resource
    private IStaffService iStaffService;
    @Resource
    private IProjectService iProjectService;
    @Resource
    private IOrganization iOrganization;
    /**
     * 通过设备编号获取设备信息接口
     */
    @ApiOperation(value = "通过设备编号获取设备信息", notes = "通过设备编号获取设备信息")
    @RequestMapping(value = "/GetEquipmentDto", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equipNo", dataType = "String", value = "设备编号")
    })
    public ResponseBean GetEquipmentDto(String equipNo) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetEquip(equipNo);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取设备信息异常"+exception);
        }
    }


    /**
     * 获取所以获取设备信息接口
     */
    @ApiOperation(value = "获取所有获取设备信息接口", notes = "获取所以获取设备信息接口")
    @RequestMapping(value = "/GetEquipmentDtoList", method = RequestMethod.GET)
    public ResponseBean GetEquipmentDtoList(@Valid EquipmentPageReq equipmentPageReq) {

        // ResponseDataModel<PageInfo> response = new ResponseDataModel<>();
        try {
            if (equipmentPageReq.getCurrPage() == null || equipmentPageReq.getPageSize() == null) {
                equipmentPageReq.setCurrPage(1);
                equipmentPageReq.setPageSize(10);
            }
            //查询数据
            List<Map<String, Object>> list = iAppApiService.EquipmentDtoList(equipmentPageReq);

            //调用分页查询的方
            PageInfo pageInfo = new PageInfo(list, equipmentPageReq.getPageSize());//设置页面大小
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取设备信息异常");
        }
    }

    /**
     * 通过设备id获取获取安拆信息
     */
    @ApiOperation(value = "分页获取安拆信息", notes = "分页获取安拆信息")
    @RequestMapping(value = "/GetEquipmentdismantlList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equipId", dataType = "long", value = "设备id"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "条数")
    })
    public ResponseBean GetEquipmentdismantlList(long equipId, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//若pageNum为空
            pageNum = 1;   //设置默认当前页为1
        }
        if (pageNum <= 0) {  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if (pageSize == null) { //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }
        ResponseDataModel<PageInfo> response = new ResponseDataModel<>();
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetDism(equipId, pageNum, pageSize);

            //调用分页查询的方
            PageInfo pageInfo = new PageInfo(list, pageSize);//设置页面大小


            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取获取安拆信息异常");
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

    }

    /**
     * 通过设备编号获取设备自检信息
     */
    @ApiOperation(value = "分页获取自检信息", notes = "分页获取自检信息")
    @RequestMapping(value = "/GetSelfcheckList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equip_no", dataType = "String", value = "设备编号"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "条数")
    })
    public ResponseBean GetSelfcheckList(String equip_no, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//若pageNum为空
            pageNum = 1;   //设置默认当前页为1
        }
        if (pageNum <= 0) {  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if (pageSize == null) { //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }
        try {
            List<Map<String, Object>> list = iAppApiService.GetSelf(equip_no, pageNum, pageSize);
            PageInfo pageInfo = new PageInfo(list, pageSize);
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取设备自检信息异常");
        } finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
    }

    /**
     * 根据项目,操作人员,设备类型获取所有自检信息
     */
    @ApiOperation(value = "项目,操作人员,类型获取所有自检信息", notes = "项目,操作人员,类型获取所有自检信息")
    @RequestMapping(value = "/GetSelfcheckPageList", method = RequestMethod.GET)
    public ResponseBean GetSelfcheckPageList(@Valid AppSelfcheckPageReq appSelfcheckPageReq) {
        try {
            if (appSelfcheckPageReq.getCurrPage() == null || appSelfcheckPageReq.getPageSize() == null) {
                appSelfcheckPageReq.setCurrPage(1);
                appSelfcheckPageReq.setPageSize(10);
            }
            //查询数据
            List<Map<String, Object>> list = iAppApiService.AppSelfcheckPageList(appSelfcheckPageReq);

            //调用分页查询的方
            PageInfo pageInfo = new PageInfo(list, appSelfcheckPageReq.getPageSize());//设置页面大小


            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取所有自检信息异常");
        }
    }

    /**
     * 分页获取设备巡检信息
     */
    @ApiOperation(value = "分页获取设备巡检信息", notes = "分页获取设备巡检信息")
    @RequestMapping(value = "/GetOnsiteinspectionList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equip_id", dataType = "long", value = "巡检设备"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "条数")
    })
    public ResponseBean GetOnsiteinspectionList(long equip_id, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//若pageNum为空
            pageNum = 1;   //设置默认当前页为1
        }
        if (pageNum <= 0) {  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if (pageSize == null) { //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }
        try {
            List<Map<String, Object>> list = iAppApiService.GetOnsiteinspection(equip_id, pageNum, pageSize);
            PageInfo pageInfo = new PageInfo(list, pageSize);
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取设备巡检信息异常");
        }
    }

    /**
     * 根据项目和人员获取所有巡检信息
     */
    @ApiOperation(value = "项目,操作人员,类型获取所有巡检信息", notes = "项目,操作人员,类型获取所有巡检信息")
    @RequestMapping(value = "/GetOnsiteinspectionPageList", method = RequestMethod.GET)
    public ResponseBean GetOnsiteinspectionPageList(@Valid OnsiteinspectionPageReq onsiteinspectionPageReq) {

        try {
            if (onsiteinspectionPageReq.getCurrPage() == null || onsiteinspectionPageReq.getPageSize() == null) {
                onsiteinspectionPageReq.setCurrPage(1);
                onsiteinspectionPageReq.setPageSize(10);
            }
            //查询数据
            List<Map<String, Object>> list = iAppApiService.OnsiteinspectionPageList(onsiteinspectionPageReq);

            //调用分页查询的方
            PageInfo pageInfo = new PageInfo(list, onsiteinspectionPageReq.getPageSize());//设置页面大小

            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取巡检信息异常");
        }
    }

    /**
     * 新增巡检信息接口
     */
    @ApiOperation(value = "新增巡检信息接口", notes = "新增巡检信息接口")
    @PostMapping(value = "/NewOnsiteinspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "OnsiteinspectionDto", dataType = "object", value = "自检信息参数")
    })
    public ResponseModel NewOnsiteinspection(@Validated @RequestBody OnsiteinspectionDto onsiteinspectionDto) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            int result = iAppApiService.InsertOnsiteinspection(onsiteinspectionDto);
            responseModel.setMessage("新增巡检信息成功");
            return responseModel;
        } catch (BusinessException exception) {
            responseModel.setMessage("新增巡检信息异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
            return responseModel;
        }
    }

    /**
     * 获取设备月检信息
     */
    @ApiOperation(value = "分页获取设备月检信息", notes = "分页获取设备月检信息")
    @RequestMapping(value = "/GetMonthinspectionList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equip_id", dataType = "long", value = "巡检设备"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "条数")
    })
    public ResponseBean GetMonthinspectionList(long equip_id, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//若pageNum为空
            pageNum = 1;   //设置默认当前页为1
        }
        if (pageNum <= 0) {  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if (pageSize == null) { //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }

        try {
            List<Map<String, Object>> list = iAppApiService.GetMonthinspection(equip_id, pageNum, pageSize);
            PageInfo pageInfo = new PageInfo(list, pageSize);
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("分页获取设备月检信息");
        }
    }

    /**
     * 根据月检项目和操作人员获取所有月检信息
     */
    @ApiOperation(value = "项目,操作人员,类型获取所有月检信息", notes = "项目,操作人员,类型获取所有月检信息")
    @RequestMapping(value = "/GetMnthinspectionPageList", method = RequestMethod.GET)
    public ResponseBean GetMonthinspectionPageList(@Valid MonthinspectionPageReq monthinspectionPageReq) {
        try {
            if (monthinspectionPageReq.getCurrPage() == null || monthinspectionPageReq.getPageSize() == null) {
                monthinspectionPageReq.setCurrPage(1);
                monthinspectionPageReq.setPageSize(10);
            }
            //查询数据
            List<Map<String, Object>> list = iAppApiService.MonthinspectionPageList(monthinspectionPageReq);

            //调用分页查询的方
            PageInfo pageInfo = new PageInfo(list, monthinspectionPageReq.getPageSize());//设置页面大小

            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取所有自检信息异常");
        }
    }

    /**
     * 新增月检信息接口
     */
    @ApiOperation(value = "新增月检信息接口", notes = "新增月检信息接口")
    @PostMapping(value = "/NewMonthinspection")
    public ResponseModel NewMonthinspection(@Validated @RequestBody MonthinspectionDto monthinspectionDto) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            //int result = iAppApiService.InsertmonthinspectionDto(monthinspectionDto);

            responseModel.setMessage("新增月检成功");
            return responseModel;
        } catch (BusinessException exception) {
            responseModel.setMessage("新增月检异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
            return responseModel;
        }
    }

    /**
     * 编辑月检信息
     */
    @ApiOperation(value = "编辑月检信息", notes = "编辑月检信息")
    @PostMapping(value = "/updateMonthinspection")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "monthinspectionDto", dataType = "object", value = "月检参数")
    })
    public ResponseModel updateMonthinspection(MonthinspectionDto monthinspectionDto) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            iAppApiService.UpdateMonthinspectionDto(monthinspectionDto);

        } catch (appapi.Global.BusinessException exception) {

            responseModel.setMessage("编辑整改信息异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 登录
     *
     * @return
     * @auther lc 2023/5/09
     */
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @RequestMapping(value = "/Staff", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mobile", dataType = "String", value = "电话"),
            @ApiImplicitParam(paramType = "query", name = "password", dataType = "String", value = "密码")
    })
    public ResponseModel StaffLogin(String mobile, String password) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            //查询数据
            Map<String, Object> result = loginService.CheckLogin(mobile, password);

            responseModel.setMessage("登录成功");
            responseModel.setData(result);
        } catch (appapi.Global.BusinessException exception) {
            responseModel.setMessage("登录异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 重置密码
     */
    @ApiOperation(value = "重置密码", notes = "重置密码")
    @PostMapping(value = "/resetPwd")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "staffId", dataType = "long", value = "管理人员Id")
    })
    public ResponseModel resetPwd(long staffId) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            int result = iStaffService.ResetPwd(staffId);
            return responseModel;
        } catch (BusinessException exception) {

            responseModel.setMessage("删除用户异常: ".concat(exception.getMessage()));
            return responseModel;
        }
    }

    /**
     * 修改管理人员
     */
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PostMapping(value = "/updateStaff")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "staffDto", dataType = "object", value = "管理人员参数")
    })
    public ResponseBean updateStaff(StaffDto staffDto) {
        try {
            int result = iStaffService.UpdateStaff(staffDto);
            return ResponseBean.success();
        } catch (BusinessException exception) {

            return ResponseBean.fail("修改用户异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 获取管理人员信息
     */
    @ApiOperation(value = "获取管理人员信息", notes = "获取管理人员信息")
    @GetMapping(value = "/GetStaff")
    public ResponseBean GetStaff(Long staffId) {
        try {
            Map<String, Object> staffDto = iStaffService.SelectStaffDetail(staffId);

            return ResponseBean.success(staffDto);
        } catch (BusinessException exception) {
            return ResponseBean.fail("查询管理人员详情异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 获取设备整改维修
     */
    @ApiOperation(value = "分页获取设备整改维修", notes = "分页获取设备整改维修")
    @RequestMapping(value = "/GetRectificationList", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equipId", dataType = "long", value = "整改设备"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", dataType = "int", value = "页数"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "int", value = "条数")
    })
    public ResponseBean GetRectificationList(long equipId, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {//若pageNum为空
            pageNum = 1;   //设置默认当前页为1
        }
        if (pageNum <= 0) {  //若为负数
            pageNum = 1;   //设置默认值为1
        }
        if (pageSize == null) { //若页面大小为空
            pageSize = 5;    //设置默认每页显示的数据数
        }
        ResponseDataModel<PageInfo> response = new ResponseDataModel<>();
        try {
            List<Map<String, Object>> list = iAppApiService.GetRectificationList(equipId, pageNum, pageSize);
            PageInfo pageInfo = new PageInfo(list, pageSize);
            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("查询管理人员详情异常");
        }
    }

    /**
     * 编辑整改信息
     *
     * @return
     * @auther 2023/5/12
     */
    @ApiOperation(value = "编辑整改信息", notes = "编辑整改信息")
    @PostMapping(value = "/updateRectification")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "rectificationDto", dataType = "object", value = "整改参数")
    })
    public ResponseModel updateRectification(RectificationDto rectificationDto) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            iAppApiService.UpdateRectification(rectificationDto);

        } catch (appapi.Global.BusinessException exception) {

            responseModel.setMessage("编辑整改信息异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 获取设备类型下拉接口
     */
    @ApiOperation(value = "获取设备类型下拉接口", notes = "获取设备类型下拉接口")
    @RequestMapping(value = "/GetEquipmenttype", method = RequestMethod.GET)
    public ResponseBean GetEquipmenttype() {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetEquipmenttype();
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("设备类型下拉异常");
        }
    }

    /**
     * 获取数据字典
     */
    @ApiOperation(value = "获取数据字典", notes = "获取数据字典")
    @RequestMapping(value = "/GetDicType", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "DicType", dataType = "String", value = "类别"),
    })
    public ResponseBean GetDicType(String DicType) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetDicType(DicType);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取数据字典异常");
        }
    }

    /**
     * 获取所以操作人员
     */
    @ApiOperation(value = "获取操作人员下拉框", notes = "获取操作人员下拉框")
    @RequestMapping(value = "/Getoperator", method = RequestMethod.GET)
    public ResponseBean Getoperator() {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.Getoperator();
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取操作人员下拉框异常");
        }
    }

    /**
     * 获取所有项目下拉框
     */
    @ApiOperation(value = "获取项目下拉框", notes = "获取项目下拉框")
    @RequestMapping(value = "/GetProject", method = RequestMethod.GET)
    public ResponseBean GetProject(ProjPageReq projPageReq) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetProject(projPageReq);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取项目下拉框异常");
        }
    }

    /**
     * 根据项目,操作人员,设备类型获取所有维保信息
     */
    @ApiOperation(value = "项目,操作人员,类型获取所有维保信息", notes = "项目,操作人员,类型获取所有维保信息")
    @RequestMapping(value = "/GetEquipmentmaintenanList", method = RequestMethod.GET)
    public ResponseBean GetEquipmentmaintenanList(EquipmentmaintenanPageReq equipmentmaintenanPageReq) {

        try {
            if (equipmentmaintenanPageReq.getCurrPage() == null || equipmentmaintenanPageReq.getPageSize() == null) {
                equipmentmaintenanPageReq.setCurrPage(1);
                equipmentmaintenanPageReq.setPageSize(10);
            }
            //查询数据
            List<Map<String, Object>> list = iAppApiService.EquipmentmaintenanPageList(equipmentmaintenanPageReq);

            //调用分页查询的方
            PageInfo pageInfo = new PageInfo(list, equipmentmaintenanPageReq.getPageSize());//设置页面大小


            return ResponseBean.success(pageInfo);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取所有自检信息异常");
        }
    }

    /**
     * 新增维保息
     */
    @ApiOperation(value = "新增维保息", notes = "新增维保息")
    @PostMapping(value = "/newEquipmentmaintenan")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "equipmentmaintenanDto", dataType = "object", value = "维保参数")
    })
    public ResponseBean newEquipmentmaintenan(EquipmentmaintenanDto equipmentmaintenanDto) {
        try {
            iAppApiService.InsertEquipmentmaintenan(equipmentmaintenanDto);
            return ResponseBean.success();
        } catch (appapi.Global.BusinessException exception) {
            return ResponseBean.fail("编辑整改信息异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 编辑维保息
     */
    @ApiOperation(value = "编辑维保信息", notes = "编辑维保信息")
    @PostMapping(value = "/updateEquipmentmaintenan")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "equipmentmaintenanDto", dataType = "object", value = "维保参数")
    })
    public ResponseModel updateEquipmentmaintenan(EquipmentmaintenanDto equipmentmaintenanDto) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            iAppApiService.UpdateEquipmentmaintenan(equipmentmaintenanDto);

        } catch (appapi.Global.BusinessException exception) {

            responseModel.setMessage("编辑整改信息异常: ".concat(exception.getMessage()));
            responseModel.setCode(exception.getCode());
        }
        return responseModel;
    }

    /**
     * 用前端请求接口获取的code换取用户手机号
     *
     * @param weChatPhone
     * @return
     */
    @ApiOperation(value = "获取手机号", notes = "获取手机号")
    @PostMapping("/phone")
    public ResponseDataModel getPhone(WeChatPhoneDTO weChatPhone) {
        ResponseDataModel responseModel = new ResponseDataModel();
        try {


            String Appid = "wx55211786a6637654";
            String SecretKey = "59d412cd423c39e340964818115291d6";

            // 1.请求微信接口服务，获取accessToken
            JSONObject accessTokenJson = WeChatUtil.getAccessToken(Appid, SecretKey);
            String accessToken = accessTokenJson.get("access_token", String.class);
            // 2.请求微信接口服务，获取用户手机号信息
            JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);
            WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);

            responseModel.setData(phoneInfo.getPurePhoneNumber());

            return responseModel;
        } catch (BusinessException exception) {
            responseModel.setMessage("获取手机号异常: ".concat(exception.getMessage()));
            return responseModel;
        }
    }
//    /**
//     * 用前端请求接口获取的code获取微信号
//     *
//     * @param weChatPhone
//     * @return
//     */
//    @ApiOperation(value = "获取微信号", notes = "获取微信号")
//    @PostMapping("/getWXID")
//    public ResponseDataModel getWXID(WeChatPhoneDTO weChatPhone) {
//        ResponseDataModel responseModel = new ResponseDataModel();
//        try {
//            String Appid = "wx55211786a6637654";
//            String SecretKey = "59d412cd423c39e340964818115291d6";
//
//
//            GetOpenIdUtil getOpenIdUtil=new GetOpenIdUtil();
//            //String jsonId = getOpenIdUtil.getopenid(Appid,weChatPhone.getCode(),SecretKey);
//            // JSONObject jsonObject = JSONObject.parseObject(jsonId);
//            //System.out.println(jsonId);
//            responseModel.setData(jsonId);
//            return responseModel;
//        } catch (BusinessException exception) {
//            responseModel.setMessage("获取手机号异常: ".concat(exception.getMessage()));
//            return responseModel;
//        }
//    }
//    /**
//     * 获取手机号后查询是否有此手机号返回登录是否成功
//     *
//     * @return
//     */
//    @ApiOperation(value = "查询是否拥有此用户", notes = "查询是否拥有此用户")
//    @RequestMapping(value = "/GetUser", method = RequestMethod.GET)
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "staffMobile", dataType = "String", value = "手机号")
//    })
//    public ResponseModel GetUser(String staffMobile,String wxid) {
//        ResponseDataModel<List<Map<String, Object>>> response = new ResponseDataModel<>();
//        try {
//            if (staffMobile==null || wxid==null){
//                response.setMessage("登录失败,手机号或微信号不能为空");
//                return response;
//            }
//            //查询数据
//            List<Map<String, Object>> list = iAppApiService.GetMobile(staffMobile,wxid);
//            int count = list.size();
//            if (count > 0) {
//                response.setCode(200);
//                response.setRcount(count);
//                response.setMessage("查询成功");
//                response.setData(list);
//            } else {
//                response.setCode(400);
//                response.setRcount(count);
//                response.setMessage("查询失败,无此手机号");
//            }
//
//            return response;
//        } catch (BusinessException exception) {
//            response.setMessage("异常: ".concat(exception.getMessage()));
//            return response;
//        }
//    }
//    @ApiOperation(value = "新增巡检以及子表", notes = "新增巡检以及子表")
//    @RequestMapping(value = "/PostOnsi", method = RequestMethod.POST)
//    public ResponseModel PostOnsi(@RequestBody JSONObject json){
//       // System.out.println("username:"+json.get("role"));
//        JSONObject list = iAppApiService.GetOnsi(json);
//        ResponseDataModel responseModel = new ResponseDataModel();
//        return responseModel;
//    }

    /**
     * 新增巡检以及子表
     */
    @PostMapping(value = "/PostOnsi")
    @ApiOperation(value = "新增巡检以及子表")
    public ResponseModel PostOnsi(@Validated @RequestBody AddOnsiteinspectionVo addOnsiteinspectionVo) {
        addOnsiteinspectionVo.setCreateTime(DateTime.now());
        addOnsiteinspectionVo.setOnsiteDate(DateTime.now());
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            long size = iAppApiService.InsertAddoOnsit(addOnsiteinspectionVo);
            int count = (int) size;
            responseModel.setCode(200);
            responseModel.setRcount(count);
        } catch (BusinessException exception) {
            responseModel.setMessage("新增巡检表异常: ".concat(exception.getMessage()));
        }
        return responseModel;
    }

//    /**
//     * 新增月检以及子表
//     */
//    @PostMapping(value = "/Postmonth")
//    @ApiOperation(value = "新增月检以及子表")
//    public ResponseModel Postmonth(@Validated @RequestBody AddMonthinspectionVo addMonthinspectionVo) {
//        addMonthinspectionVo.setCreateTime(DateTime.now());
//        addMonthinspectionVo.setMonthDate(DateTime.now());
//        ResponseDataModel responseModel = new ResponseDataModel();
//        try {
//            long size = iAppApiService.InsertAddMonth(addMonthinspectionVo);
//            int count = (int) size;
//            responseModel.setCode(200);
//            responseModel.setRcount(count);
//        } catch (BusinessException exception) {
//            responseModel.setMessage("新增月检表异常: ".concat(exception.getMessage()));
//        }
//        return responseModel;
//    }

    /**
     * 新增自检以及子表
     */
    @PostMapping(value = "/PostSelfcheck")
    @ApiOperation(value = "新增自检以及子表")
    public ResponseModel PostSelfcheck(@Validated @RequestBody AddSelfcheckVo addSelfcheckVo) {
//        addSelfcheckVo.setCreateTime(DateTime.now());
//        addSelfcheckVo.setMonthDate(DateTime.now());
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            long size = iAppApiService.InsertAddSelfcheck(addSelfcheckVo);
            int count = (int) size;
            responseModel.setCode(200);
            responseModel.setRcount(count);
        } catch (BusinessException exception) {
            responseModel.setMessage("新增自检表异常: ".concat(exception.getMessage()));
        }
        return responseModel;
    }

    /**
     * 查看点击设备巡检结果详情
     */
    @ApiOperation(value = "获取数据字典", notes = "获取数据字典")
    @RequestMapping(value = "/GetOnSite", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "DicType", dataType = "String", value = "类别"),
    })
    public ResponseBean GetOnSite(String DicType) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetDicType(DicType);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取数据字典异常");
        }
    }

    /**
     * 多个修改整改信息
     */
    @PostMapping(value = "/PostRectificationList")
    @ApiOperation(value = "多个修改整改信息", notes = "多个修改整改信息")
    public ResponseModel PostRectificationList(@Validated @RequestBody updateRectificationListVo up) {
//        addSelfcheckVo.setCreateTime(DateTime.now());
//        addSelfcheckVo.setMonthDate(DateTime.now());
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            long size = iAppApiService.updateRectificationList(up);
            int count = (int) size;
            responseModel.setCode(200);
            responseModel.setRcount(count);
        } catch (BusinessException exception) {
            responseModel.setMessage("修改整改表异常: ".concat(exception.getMessage()));
        }
        return responseModel;
    }

    /**
     * 获取设备类型,添加维保项目信息
     */
    @ApiOperation(value = "获取设备类型,添加维保项目信息", notes = "获取设备类型,添加维保项目信息")
    @RequestMapping(value = "/GetMaintenanitem", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "equipTypeId", dataType = "long", value = "设备类型id"),
    })
    public ResponseBean GetMaintenanitem(long equipTypeId) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetMaintenanitem(equipTypeId);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取设备类型异常");
        }
    }

    /**
     * 新增维保到maintenandetail
     */
    @PostMapping(value = "/PostMaintenandetailList")
    @ApiOperation(value = "新增维保记录详情", notes = "新增维保记录详情")
    public ResponseModel PostMaintenandetailList(@Validated @RequestBody AddMaintenandetailListVo add) {
//        addSelfcheckVo.setCreateTime(DateTime.now());
//        addSelfcheckVo.setMonthDate(DateTime.now());
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            long size = iAppApiService.AddMaintenandetailList(add);
            int count = (int) size;
            responseModel.setCode(200);
            responseModel.setRcount(count);
        } catch (BusinessException exception) {
            responseModel.setMessage("新增维保异常: ".concat(exception.getMessage()));
        }
        return responseModel;
    }
//
//    /**
//     * 小程序首页
//     */
//    @GetMapping(value = "/GetWxHomePage")
//    @ApiOperation(value = "小程序首页", notes = "小程序首页")
//    public ResponseBean GetWxHomePage(HttpServletRequest request, Long projId) {
//        try {
//            List<WxHomePageVo> vos = iAppApiService.GetWxHomePage(request,projId);
//            return ResponseBean.success(vos);
//        } catch (BusinessException exception) {
//            return ResponseBean.fail("小程序首页获取异常: ".concat(exception.getMessage()));
//        }
//    }
    /**
     * 通过巡检记录id获取巡检明细
     */
    @ApiOperation(value = "获取巡检明细", notes = "获取巡检明细")
    @RequestMapping(value = "/GetOnsitedetail", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "onsiteId", dataType = "String", value = "记录ID")
    })
    public ResponseBean GetOnsitedetail(long onsiteId) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetOnsitedetailList(onsiteId);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取巡检明细异常");
        }
    }
    /**
     * 通过月检记录id获取月检明细
     */
    @ApiOperation(value = "获取月检明细", notes = "获取月检明细")
    @RequestMapping(value = "/GetMonthdetail", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "monthId", dataType = "String", value = "月检记录ID")
    })
    public ResponseBean GetMonthdetail(long monthId) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.GetMonthdetailList(monthId);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取月检明细异常");
        }
    }
    /**
     * 通过维保ID获取维保信息
     */
    @ApiOperation(value = "获取维保记录详情", notes = "获取维保记录详情")
    @RequestMapping(value = "/Getmaintenandetail", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mainId", dataType = "String", value = "维保id")
    })
    public ResponseBean Getmaintenandetail(long mainId) {
        try {
            //查询数据
            List<Map<String, Object>> list = iAppApiService.Getmaintenandetail(mainId);
            return ResponseBean.success(list);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取维保记录详情异常");
        }
    }
    /**
     * 新增整改信息
     */
    @PostMapping(value = "/PostRectificationAll")
    @ApiOperation(value = "新增整改信息", notes = "新增整改信息")
    public ResponseModel PostRectificationAll(@Validated @RequestBody AddRectificationVo addRectification) {
//        addSelfcheckVo.setCreateTime(DateTime.now());
//        addSelfcheckVo.setMonthDate(DateTime.now());
        ResponseDataModel responseModel = new ResponseDataModel();
        try {
            long size = iAppApiService.AddRectificationAll(addRectification);
            int count = (int) size;
            responseModel.setCode(200);
            responseModel.setRcount(count);
        } catch (BusinessException exception) {
            responseModel.setMessage("新增整改表异常: ".concat(exception.getMessage()));
        }
        return responseModel;
    }
    /**
     * 获取项目选择列表
     *
     * @return
     * @auther lc 2023/5/12
     */
    @ApiOperation(value = "获取所有项目", notes = "获取所有项目")
    @GetMapping(value = "/GetProjectList")
    public ResponseBean GetProjectList(@Valid ProjReq req) {
        try {
            List<Map<String, Object>> projList = iProjectService.getProjectList(req);
            return ResponseBean.success(projList);
        } catch (Exception e) {
            return ResponseBean.fail("获取项目下拉框失败");
        }
    }

//    /**
//     * 获取组织机构
//     */
//    @ApiOperation(value = "组织机构树查询", notes = "组织机构树查询")
//    @GetMapping(value = "/GetOrganizationTree")
//    public ResponseBean GetOrganizationTree(@CurrentUser Long orgaPid) {
//        try {
//            List<Map<String, Object>> tree = iOrganization.SelectOrgTree(orgaPid);
//
//            return ResponseBean.success(tree);
//        } catch (Exception e) {
//            return ResponseBean.fail("组织机构树查询异常");
//        }
//    }
    /**
     * 获取组织机构详情
     *
     */
    @ApiOperation(value = "获取组织机构详情", notes = "获取组织机构详情")
    @GetMapping(value = "/GetOrganizationById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orgaId", dataType = "long", value = "组织机构Id")
    })
    public ResponseBean GetOrganizationById(Long orgaId) {
        try {
            Map<String, Object> map = iOrganization.SelectOrgDetail(orgaId);
            return ResponseBean.success(map);
        } catch (appapi.Global.BusinessException exception) {
            return ResponseBean.fail("获取组织机构详情异常");
        }
    }
}
